#ifndef LIBCO_EPOLLEVENT_H
#define LIBCO_EPOLLEVENT_H

#include "sys/epoll.h"
#include "unistd.h"
#include <functional>
#include <cstdint>
#include "mutex.hpp"
#include "macro.h"

namespace libco {

extern uint64_t thread_local t_id;

enum EpollEvent {
    /// 无事件
    NONE = 0b0,
    /// 读事件(EPOLLIN)
    READ = 0b1,
    /// 写事件(EPOLLOUT)
    WRITE = 0b100,
};

struct FdContext {
    std::function<void()> read_cb;
    std::function<void()> write_cb;
    int fd;
    uint64_t proc_id;
    EpollEvent events;
    Mutex mu;
};

class EpollManager {
    
protected:
    int m_epfd;
    Mutex m_mutex;
    std::unordered_map<int, FdContext*> m_fd_ctxs;

public:
    EpollManager();
    ~EpollManager();

    void add_event(int fd, EpollEvent events, std::function<void()> cb);
    void del_event(int fd);
    size_t GetPendingEvent();
};

} // namespace libco

#endif // LIBCO_EPOLLEVENT_H